package com.student91.recruit.controller;

import com.student91.recruit.dao.RegisterMapper;
import com.student91.recruit.entity.Register;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by bizihang
 * 2017/8/22
 */
@Controller
public class IndexController {

    @Autowired
    RegisterMapper registerMapper;
//
//    @Autowired
//    ProxyMapper proxyMapper;
//
//    @Autowired
//    PartyInfoMapper partyInfoMapper;
//
//    @Autowired
//    PositionMapper positionMapper;
//
//    @Autowired
//    HotelMapper hotelMapper;
//
//    @Autowired
//    RoomMapper roomMapper;

//    final static Logger logger = Logger.getLogger(IndexController.class.toString());

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/wangshangbaoming")
    public String step2(Register register,ModelMap map) {
        Register r=registerMapper.findByName(register.getName());
        if (r==null) {
                registerMapper.save(register);
        } else {
            throw new RuntimeException("您已经登记，不能重复登记；如果想要修改，请电话联系管理员!");
        }
        map.addAttribute("msg","报名成功！");
        return "success";
    }

    @RequestMapping("/findOne")
    public String findOne(String name,ModelMap map){
        map.addAttribute("register",registerMapper.findByName(name));
        return "view";
    }

//    @GetMapping("/yibaoming")
//    public String findAll(ModelMap map){
//        List<Register> registers= registerMapper.findAll();
//        map.addAttribute("registers",registers);
//        return "baoming_list";
//    }

    @GetMapping("/yibaoming")
    public String getRegisters(ModelMap map,@RequestParam(name = "index", defaultValue = "0") Integer start) {
        Page<Register> page = null;
        Pageable pageable = new PageRequest(start, 10, Sort.Direction.DESC, "id");
        page = registerMapper.findAll(pageable);
        map.addAttribute("page",page);
        map.addAttribute("start",start);
        return "baoming_list";
    }

//    @RequestMapping("/step3")
//    public String step3(ProxyVo proxyList, HttpSession session) {
//        //保存参会代表信息
//        Register loadFromSession = loadFromSession(session);
//        proxyList.getProxyList().removeIf(proxy -> !StringUtils.hasText(proxy.getName()));
//        proxyList.getProxyList().forEach(proxy -> {
//            proxy.setRegister(loadFromSession.getId());
//            proxyMapper.save(proxy);
//        });
//        //查询酒店名称
//        List<Hotel> hotels= hotelMapper.findAll();
//        session.setAttribute("hotels", hotels);
//        return "redirect:/step3.jsp";
//    }

//    @RequestMapping("/step4")
//    public String step4(PartyInfo partyInfo, HttpSession session) {
//        //参会情况保存
//        Register loadFromSession = loadFromSession(session);
//        partyInfo.setRegister(loadFromSession.getId());
//        partyInfo.setPostNum("预定展位："+partyInfo.getPostNum());
//        partyInfo.setNum("参会人数："+partyInfo.getNum());
//        partyInfo.setManNum("男："+partyInfo.getManNum());
//        partyInfo.setRemanNum("女："+partyInfo.getRemanNum());
//        partyInfo.setVehicle("交通工具："+partyInfo.getVehicle());
//        partyInfo.setTrainnumber("所乘车次："+partyInfo.getTrainnumber());
//        partyInfo.setStation("到达站点："+partyInfo.getStation());
//        partyInfo.setArrivaltime("到站时间："+partyInfo.getArrivaltime());
//        partyInfoMapper.save(partyInfo);
//        return "redirect:/step4.jsp";
//    }

//    @RequestMapping("/step5")
//    public String step4(PositionVo positionVo, HttpSession session) {
//        //招聘职位
//        Register loadFromSession = loadFromSession(session);
//        positionVo.getPositionList().removeIf(position -> !StringUtils.hasText(position.getName()));
//        positionVo.getPositionList().forEach(position -> {
//            position.setRegister(loadFromSession.getId());
//            position.setName("招聘职位："+position.getName());
//            position.setYear("工作年限："+position.getYear());
//            position.setNumber("招聘人数："+position.getNumber());
//            position.setAge("年龄："+position.getAge());
//            position.setEducation("文化程度："+position.getEducation());
//            position.setPrefessional("所学专业："+position.getPrefessional());
//            position.setAddress("工作地址："+position.getAddress());
//            positionMapper.save(position);
//        });
//        session.invalidate();
//        return "redirect:/success.jsp";
//    }

//    @RequestMapping("/hotel")
//    @ResponseBody
//    public List<Room> findHotel(String hname){
//
//        return roomMapper.findByHname(hname);
//    }

//    @RequestMapping("/type")
//    @ResponseBody
//    public Room findPrice(String type){
//
//        return roomMapper.findByType(type);
//    }

//    @RequestMapping("/export")
//    public void export(HttpServletResponse response) throws Exception {
//        String fileName = "bm.xls";
//        response.reset();
//        response.setContentType("application/octet-stream; charset=utf-8");
//        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
//        ServletOutputStream outputStream = response.getOutputStream();
//        loadData(outputStream);//加载数据
//        outputStream.flush();
//        outputStream.close();
//        return;
//    }

//    @RequestMapping("/export2")
//    public ResponseEntity<byte[]> download() throws Exception {
//        String fileName = "bm.xls";
//        String downloadFileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//        headers.setContentDispositionFormData("attachment", downloadFileName);
//        XLSExportUtil exportUtil = new XLSExportUtil(fileName, "aa");
//        exportUtil.exportXLS();
//        return new ResponseEntity<byte[]>(null, headers, HttpStatus.CREATED);
//    }


//    private void loadData(OutputStream outputStream) {
//        HSSFWorkbook wb = new HSSFWorkbook();
//        Sheet sheet = wb.createSheet();
//        Row firstRow = sheet.createRow(0);
//        setTitle(firstRow);//设置标题
//
//        //获取所有数据
//        List<Register> allRegister = registerMapper.findAll();
//        for (int i = 0; i < allRegister.size(); i++) {
//            Register register = allRegister.get(i);
//            Row row = sheet.createRow(i + 1);
//            setRegister(row, register);//设置基本公司
//
//            //参会人员
//            List<Proxy> proxies = proxyMapper.findAllByRegister(register.getId());
//            setProxy(row, proxies);
//
//            //参会情况 这里有个 bug
//            PartyInfo partyInfo = partyInfoMapper.findFirstByRegister(register.getId());
//            if (Objects.nonNull(partyInfo)) {
//                setPartInfo(row, partyInfo.toString());
//            } else {
//                setPartInfo(row, new PartyInfo().toString());
//            }
//
//            //招聘情况
//            List<Position> positions = positionMapper.findAllByRegister(register.getId());
//            setPosition(row, positions);
//        }
//
//        try {
//            wb.write(outputStream);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        } finally {
//            if (Objects.nonNull(wb)) {
//                try {
//                    wb.close();
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }
//    }
//
//    private void setRegister(Row row, Register register) {
//        createCell(row, 0, register.getName());
//        createCell(row, 1, register.getTel());
//        createCell(row, 2, register.getFax());
//        createCell(row, 3, register.getEmail());
//        createCell(row, 4, register.getAddress());
//        createCell(row, 5, register.getPost());
//        createCell(row, 6, register.getOrganization());
//    }
//
//    private void setProxy(Row row, List<Proxy> proxies) {
//        createCell(row, 7, Arrays.toString(proxies.toArray()));
//    }
//
//    private void setPartInfo(Row row, String str) {
//        //这里有一个bug:partyInfo.toString
//        createCell(row, 8, str);
//    }
//
//    private void setPosition(Row row, List<Position> positionList) {
//        createCell(row, 9, Arrays.toString(positionList.toArray()));
//    }
//
//    private void setTitle(Row row) {
//        row.createCell(0).setCellValue("单位名称");
//        row.createCell(1).setCellValue("联系电话");
//        row.createCell(2).setCellValue("传真号码");
//        row.createCell(3).setCellValue("电子邮箱");
//        row.createCell(4).setCellValue("单位地址");
//        row.createCell(5).setCellValue("邮政编码");
//        row.createCell(6).setCellValue("机构代码");
//        row.createCell(7).setCellValue("参会代表信息");//多
//        row.createCell(8).setCellValue("参会情况");//多
//        row.createCell(9).setCellValue("招聘需求");//多
//    }
//
//    private void createCell(Row row, int index, String value) {
//        row.createCell(index).setCellValue(StringUtils.isEmpty(value) ? "_" : value);
//    }
//
//    private Register loadFromSession(HttpSession session) {
//        Register register = (Register) session.getAttribute("register");
//        if (Objects.isNull(register)) {
//            throw new RuntimeException();
//        }
//        return register;
//    }

}
